<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">使用者</label>
                            <div class="layui-input-inline">
                                <select name="memberId" lay-verify="" id="memberId">
                                    <option value="">请选择使用者</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">起始时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="beginDate" autocomplete="off" class="layui-input" id="beginDate">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">终止时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="endDate" autocomplete="off" class="layui-input" id="endDate">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal  layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon layui-icon-add-1"></i> 添加投资 </button>
                <a href="javascript:;" class="layui-btn layui-btn-sm  data-delete-btn" layuimini-content-href="finance/invest/type.html" data-title="投资分类管理" data-icon="fa fa-gears"><i class="layui-icon layui-icon-set-sm"></i>分类管理</a>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            {{#  if(d.flag== "0"){ }}
                {{#  if(d.status== "0"){ }}
                    <button class="layui-btn layui-btn-xs" lay-event="finish">回收</button>
                    <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">修改</button>
                    <button class="layui-btn layui-btn-danger layui-btn-xs layui-disabled" aria-disabled="true">删除</button>
                {{#  } }}
                {{#  if(d.status== "1"){ }}
                    <button class="layui-btn layui-btn-xs layui-disabled" aria-disabled="true">回收</button>
                    <button class="layui-btn layui-btn-normal layui-btn-xs layui-disabled" aria-disabled="true">修改</button>
                    <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</button>
                {{#  } }}

            {{#  } }}
            {{#  if(d.flag== "1"){ }}
            <button class="layui-btn layui-btn-xs layui-disabled" aria-disabled="true">回收</button>
            <button class="layui-btn layui-btn-normal layui-btn-xs layui-disabled" aria-disabled="true">修改</button>
            <button class="layui-btn layui-btn-danger layui-btn-xs layui-disabled" aria-disabled="true">删除</button>
            {{#  } }}
        </script>
    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element', 'axios', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            axios = layui.axios,
            laydate = layui.laydate,
            miniPage = layui.miniPage;

        laydate.render({
            elem: '#beginDate'
        })

        laydate.render({
            elem: '#endDate'
        })

        axios({
            method: 'get',
            url: 'family/member'
        }).then(response => {
            console.log(response.data)
            if (response.code == 0){
                data = response.data;
                data.forEach(elem => {
                    console.log(elem)
                    let option = `<option value="${elem.id}">${elem.name}</option>`;
                    let optionObj = $(option);
                    $("#memberId").append(optionObj);
                })

                form.render(null, "search");
            }else{
                layer.msg(response.msg)
            }
        }).catch(error => {
            console.log(error)
        })

        //渲染表格
        table.render({
            elem: '#currentTableId',
            method: 'post',
            url: 'invest/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'title', title: '投资项目', width: 150},
                {field: 'money', title: '金额', width: 100},
                {field: 'memberId', title: '投资者', width: 100, templet: function (data){
                    return data.familyMember.name;
                }},
                {field: 'startDate', width: 150, title: '投资时间', sort: true},
                {field: 'endDate', width: 150, title: '回收时间', sort: true},
                {field: 'typeId', width: 100, title: '投资类型', templet: function (data){
                    return data.investType.name
                }},
                {field: 'status', title: '状态', templet: function (data) {
                    if (data.status == 0)
                        return `<span class="layui-badge layui-bg-red">投资中</span>`;
                    return `<span class="layui-badge layui-bg-cyan">已回收</span>`;
                }},
                {field: 'remark', title: '备注信息'},
                {field: 'flag', title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('finance/invest/invest-add.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加投资信息',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            // console.log(data);
            if (obj.event === 'edit') {
                var content = miniPage.getHrefContent('finance/invest/invest-edit.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '编辑投资信息',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });

                form.val("editForm", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id": data.id // "name": "value"
                    ,"title": data.title
                    ,"money": data.money
                    ,"startDate": data.startDate
                    ,"typeId": data.typeId
                    ,"remark": data.remark
                });

                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    //异步请求删除
                    axios({
                        method: 'get',
                        url: 'invest/deleteInvest?ids=' + obj.data.id //将数组中的项按,分隔拼接成字符串
                    }).then(response => {
                        layer.msg(response.msg, function (){
                            layui.table.reload("currentTableId");
                            layer.close(index);
                        })
                    }).catch(error => {
                        layer.msg(error);
                    });
                });
            } else if (obj.event === "finish"){
                layer.prompt({
                    formType: 0,
                    value: '',
                    title: '请输入回收值',
                    area: ['800px', '350px'] //自定义文本域宽高
                }, function(value, index, elem){
                    // layer.alert(value + obj.data.id); //得到value
                    axios({
                        url: 'invest/retrieveInvest',
                        method: 'post',
                        data: {
                            id: obj.data.id,
                            money: value
                        }
                    }).then(response => {
                        if (response.code == 0){
                            layer.msg(response.msg);
                            layui.table.reload("currentTableId");
                        }else{
                            layer.msg(response.msg);
                        }

                    }).catch(error => {
                        layer.msg(error)
                    })
                    layer.close(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

    });
</script>